package com.nantai.jiafang.nantaijiafang.zhijian.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.ViewModel.Zhijian_Net;
import com.nantai.jiafang.nantaijiafang.activity.adapter.ZhiJianAdapter;
import com.nantai.jiafang.nantaijiafang.base.BaseFragment;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.Item.HeardItem_1;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.Item.HeardItem_2;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.MLHeardItem;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentPageBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.android.volley.VolleyLog.TAG;

/**
 * Created by Administrator on 2017/11/9.
 */

public class Pager_Fragment extends BaseFragment<FragmentPageBinding> implements InterfaceToFragment{
    private static FragmentPageBinding binding;
    public ZhiJianAdapter zhiJianAdapter;
    private  RecyclerView recyclerView;
    private  List<HeardItem_2> listt = new ArrayList<>();
    private Map<String,String> mapps=new HashMap<>();
    private RecyclerView recyclerPage2;
    static FragmentManager _fragmentManager;


    public static Pager_Fragment initdata(Map<String,String> maps, FragmentManager _fragmentManager){
        Pager_Fragment pager_fragment=new Pager_Fragment();
//        Log.e(TAG, "initUI3: " );
        pager_fragment.clearlist();
        pager_fragment.setlistt(maps);
//        pager_fragment.setfragmentmanager(_fragmentManager);
        return pager_fragment;
    }
    @Override
    public int setlayoutid() {
        return R.layout.fragment_page;
    }
    @Override
    public void initUI(FragmentPageBinding binding) {
        this.binding=binding;
        GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),1);
        binding.recyclerPage2.setLayoutManager(layoutManager);
//        if (listt==null)return;
//        Log.e(TAG, "initUI2: " );
        zhiJianAdapter=new ZhiJianAdapter(listt,R.layout.fragment_zhi_jian_item,this);
        binding.recyclerPage2.setAdapter(zhiJianAdapter);
        zhiJianAdapter.notifyDataSetChanged();
//        if (binding.recyclerPage2!=null) {
//            binding.recyclerPage2.setAdapter(zhiJianAdapter);
//        }else {
//            toast("请重新打开");
//        }
    }

    public void setlistt(Map<String,String> maps){
        this.mapps=maps;
//        if (listt!=null) return;
        Log.e(TAG, "initUI: "+mapps.get("PageIndex") );
        Zhijian_Net.getHeardItem(getContext(), mapps, new Zhijian_Net.GetSingData() {
            @Override
            public void success(Object s) {
                MLHeardItem mlHeardItemList=(MLHeardItem)s;
                listt=mlHeardItemList.getEInspectHead();

                if(zhiJianAdapter != null){
                    zhiJianAdapter.setListData(listt);
                }else{
                    zhiJianAdapter=new ZhiJianAdapter(listt,R.layout.fragment_zhi_jian_item,Pager_Fragment.this);
                    if (binding!=null&&binding.recyclerPage2!=null) {
                        binding.recyclerPage2.setAdapter(zhiJianAdapter);
                    }else {
                        //
                        // toast("请重新打开");
                    }
                }

//                zhiJianAdapter=new ZhiJianAdapter(listt,R.layout.fragment_zhi_jian_item,Pager_Fragment.this);
//                if (binding!=null&&binding.recyclerPage2!=null) {
//                    binding.recyclerPage2.setAdapter(zhiJianAdapter);
//                }else {
//                    //
//                    // toast("请重新打开");
//                }

            }
        });
    }
    public void setfragmentmanager(FragmentManager _fragmentManager){
        this._fragmentManager=_fragmentManager;
    }

    private void clearlist(){
        this.listt = new ArrayList<>();
    }


    @Override
    public void toZhijianFragment_detail(String id) {
        _fragmentManager.beginTransaction()
                .addToBackStack(null)
                .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                .add(R.id.container2,ZhijianFragment_detail.Initclass(id))
                .commit();
    }
    @Override
    public void toZhijianFragment_add(HeardItem_2 item) {
        getActivity().getSupportFragmentManager().beginTransaction()
                .addToBackStack(null)
                .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                .replace(R.id.container2, ZhijianFragment_add.initZhijianFragment_add(item))
                .commit();

    }

    @Override
    public void toZhijianFragment_add(HeardItem_1 item) {

    }

    public void toSwingAddFragment(HeardItem_1 item){
    getActivity().getSupportFragmentManager().beginTransaction()
                .addToBackStack(null)
                .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                .add(R.id.container3,SewingAddFragment.initSewingAddFragment(item))
                .commit();
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        _fragmentManager=null;
    }


}
